package com.twu.soloproject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: thoughtworker
 * Date: 10/10/13
 * Time: 1:18 PM
 * To change this template use File | Settings | File Templates.
 */
public class FootballLeagueManager {

    private static League league;
    public PrintStream printStream;
    public BufferedReader bufferedReader;
    private Collection<String> menuOptions = Arrays.asList("Enter 1 to get list of players in league.",
            "Enter 2 to look up a player in league.", "Enter 3 to get details of a team in league.",
            "Enter 4 to search for player info by age.", "Enter q to exit this menu.");

    public FootballLeagueManager(PrintStream printStream, BufferedReader bufferedReader, League league) {
        this.printStream = printStream;
        this.bufferedReader = bufferedReader;
        this.league = league;
    }

    public static void main (String[] args) throws IOException {
        Collection<Player> playersList = new HashSet<Player>();
        Collection<Team> teamsList = new HashSet<Team>();

        Map<String,String> coaches = new Hashtable<String, String>();
        coaches.put("Joe", "head coach");

        Team blueTeam = new Team("blue", coaches);
        Team yellowTeam = new Team("yellow", coaches);

        teamsList.add(blueTeam);
        teamsList.add(yellowTeam);


        Player johnPlayer = new Player("John", blueTeam, 1, 14);
        Player bobPlayer = new Player("Bob", yellowTeam, 2, 13);

        playersList.add(johnPlayer);
        playersList.add(bobPlayer);


        blueTeam.addPlayer(johnPlayer);
        yellowTeam.addPlayer(bobPlayer);

        League league = new League(playersList, teamsList);

        FootballLeagueManager footballLeagueManager = new FootballLeagueManager(System.out,
                new BufferedReader(new InputStreamReader(System.in)), league);

        footballLeagueManager.lookUp();

    }

    public void lookUp() throws IOException {
        String userInput = getUserInput(menuOptions);

        while (!userInput.equals("q")) {
            switch (Integer.parseInt(userInput)) {
                case 1:
                    printStream.println(league.getPlayersList());
                    break;
                case 2:
                    String playerName = getUserInput(Arrays.asList("Type a player's name to look up in league"));
                    if (league.isPlayerInLeague(playerName)) {
                        printStream.println(playerName + " is in the league.");
                    } else {
                        printStream.println(playerName + " is not in the league.");
                    }
                    break;
                case 3:
                    String teamName = getUserInput(Arrays.asList("Type a team's name to view details of its players and coaches."));
                    printStream.println(league.getTeamDetails(teamName));
                    break;
                case 4:
                    String ageSearch = getUserInput(Arrays.asList("Type an age to view details of players that are older than it."));
                    printStream.println(league.searchPlayersByAge(ageSearch));
                    break;
                default:
                    printStream.println("Please enter a valid option.");
                    break;
            }

            userInput = getUserInput(menuOptions);
        }
        printStream.println("You have exited.");

    }

    public String getUserInput(Collection<String> instructions) throws IOException {

        for (String option: instructions) {
            printStream.println(option);
        }

        return bufferedReader.readLine();
    }
}
